﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SqlMiM.ReportingServices.SqlRs;
using SqlMiM.Common.ViewModel;
using MvvmFoundation.Wpf;

namespace SqlMiM.ReportingServices.ReportExportPlugin
{
    public class CatalogItemViewModel : ObservableObject
    {
        private CatalogItem item;

        public CatalogItem Item
        {
            get { return item; }
            set { item = value; }
        }
        private ObservableCollection<CatalogItemViewModel> childrens;

        public ObservableCollection<CatalogItemViewModel> Childrens
        {
            get { return childrens; }
            set { childrens = value; }
        }
        private CatalogItemViewModel parent;

        public CatalogItemViewModel Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        
        private String name;

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        private string path;

        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        private ItemTypeEnum type;

        public ItemTypeEnum Type
        {
            get { return type; }
            set { type = value; }
        }



        private bool? isChecked = false;

        /// <summary>
        /// Gets/sets the state of the associated UI toggle (ex. CheckBox).
        /// The return value is calculated based on the check state of all
        /// child FooViewModels.  Setting this property to true or false
        /// will set all children to the same check state, and setting it 
        /// to any value will cause the parent to verify its check state.
        /// </summary>
        public bool? IsChecked
        {
            get { return isChecked; }
            set { this.SetIsChecked(value, true, false); }
        }

        private void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
        {
            if (value == isChecked)
                return;

            isChecked = value;

            if (updateChildren && isChecked.HasValue)
            {
                if (this.childrens != null)
                {
                    foreach (var c in this.childrens)
                    {
                        c.SetIsChecked(isChecked, true, false);
                    }
                }
            }
            if (updateParent && parent != null)
                parent.VerifyCheckState();

            this.RaisePropertyChanged("IsChecked");
        }

        private void VerifyCheckState()
        {
            bool? state = null;
            for (int i = 0; i < this.Childrens.Count; ++i)
            {
                bool? current = this.Childrens[i].IsChecked;
                if (i == 0)
                {
                    state = current;
                }
                else if (state != current)
                {
                    state = null;
                    break;
                }
            }
            this.SetIsChecked(state, false, false);
        }


        public CatalogItemViewModel(CatalogItem item)
        {
            this.item = item;
            this.name = item.Name;
            this.path = item.Path;
            this.type = item.Type;
               
        }



    }
}
